// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Map markers for vehicles with a name
// -----------------------------------------------

private [
	"_marker",
	"_arrayofmarker",
	"_markername",
	"_position",
	"_arrayofvehicle",
	"_counter"
];

_arrayofmarker = [];

WC_fnc_refreshvehiclelist = {
	private ["_arrayofvehicle","_arrayofvehicle2","_varname","_name"];

	_arrayofvehicle2 = [];
	_arrayofvehicle = nearestObjects [position player, ["Air", "LandVehicle"], 20000];
	{
		if !(_x isKindOf "StaticWeapon") then {
			_varname = vehicleVarName _x;
			if (_varname != "") then {
				_name = getText (configFile >> "CfgVehicles" >> (typeOf _x) >> "DisplayName");
				if (locked _x) then {
					_name = _name + " - locked";
				} else {
					_name = _name + " - unlocked";
				};
				_arrayofvehicle2 = _arrayofvehicle2 + [[_x, _varname, _name]];
			};
		};
	} foreach _arrayofvehicle;

	_arrayofvehicle2;
};

_arrayofvehicle = [] call WC_fnc_refreshvehiclelist;
{
	_marker = [(_x select 1), 2, [0,0,0], "ColorGreen", "ICON", "FDIAGONAL", "Vehicle", 0, "", true] call WC_fnc_createmarkerlocal;
	_arrayofmarker = _arrayofmarker + [_marker];
} foreach _arrayofvehicle;

_counter = 0;
while {true} do {
	{
		if (alive (_x select 0)) then {
			_markername = _x select 1;
			_position = getPos (_x select 0);
			_markername setMarkerPosLocal _position;
			_markername setMarkerTextLocal (_x select 2);
			_markername setMarkerColorLocal "ColorGreen";
			_markername setMarkerTypeLocal "Vehicle";
		}else{
			_markername = _x select 1;
			_position = getPos (_x select 0);
			_markername setMarkerPosLocal _position;
			_markername setMarkerTextLocal (_x select 2);
			_markername setMarkerColorLocal "ColorRED";
			_markername setMarkerTypeLocal "DestroyedVehicle";
		};
	} foreach _arrayofvehicle;
	_counter = _counter + 1;
	if (_counter > 60) then {
		_arrayofvehicle = [] call WC_fnc_refreshvehiclelist;
		{
			deleteMarkerLocal _x;
		} foreach _arrayofmarker;
		_arrayofmarker = [];
		{
			_marker = [(_x select 1), 2, [0,0,0], "ColorGreen", "ICON", "FDIAGONAL", "Vehicle", 0, "", true] call WC_fnc_createmarkerlocal;
			_arrayofmarker = _arrayofmarker + [_marker];
		} foreach _arrayofvehicle;
		_counter = 0;
	};
	sleep 0.5;
};